﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Indigo.Quibbler.Data;
using System.Collections;

namespace Indigo.Quibbler.Controls
{
    /// <summary>
    /// Interaction logic for SearchResultsControl.xaml
    /// </summary>
    public partial class SearchResultsControl : UserControl
    {
        #region Public Properties
        private List<NewsArticle> _itemSource;
        public List<NewsArticle> ItemSource
        {
            get { return _itemSource; }
            set
            {
                _itemSource = value;
                _SetItems();
            }
        }

        private int _selectedIndex;
        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { _selectedIndex = value; }
        }
        #endregion

        #region Delegate & Events
        public delegate void ArticleSelectedEventHandler(object sender, int selectedIndex);
        public event ArticleSelectedEventHandler ArticleSelectedEvent;
        #endregion

        #region Ctor
        public SearchResultsControl()
        {
            InitializeComponent();
        }
        #endregion

        #region Helper Functions
        private void _SetItems()
        {
            _selectedIndex = -1;
            List<ArticleControl> listArticleControl = new List<ArticleControl>();
            for (int i = 0; i < _itemSource.Count; i++)
            {
                listArticleControl.Add(_GetArticleControl(_itemSource[i], i));
            }
            ListPager lp = (ListPager)this.FindResource("listPager");
            //Screen Adjusting
            double width = System.Windows.SystemParameters.PrimaryScreenWidth;
            double height = System.Windows.SystemParameters.PrimaryScreenHeight;
            double x = width / 240;
            //double y = height / 300;
            lp.PageSize = (int)(x - 1);

            //lp.PageSize = 8;
            lp.ItemsSource = (IList)(listArticleControl).ToArray();

            prevBtn.IsEnabled = false;
            if (lp.MaxPageIndex == 0)
                nextBtn.IsEnabled = false;
            else
                nextBtn.IsEnabled = true;
            lp.CurrentPageIndex = 0;
        }
        #endregion

        #region Event Handlers
        private ArticleControl _GetArticleControl(NewsArticle newsArticle, int index)
        {
            ArticleControl articleControl = new ArticleControl();
            try
            {
                articleControl.Tag = index;
                //articleControl.Click += ArticleControlSelected;
                articleControl.Headline = newsArticle.Headline;
                articleControl.ImageUrl = newsArticle.ImageUrl;
                articleControl.SubHeadline = newsArticle.SubHeadline;
                articleControl.PubDate = newsArticle.PubDate;
                //articleControl.InitializeDocument();
            }
            catch (Exception ex) { }
            return articleControl;
        }

        void ArticleControlSelected(object sender, EventArgs e)
        {
            _selectedIndex = Convert.ToInt32((sender as ArticleControl).Tag);
            if (ArticleSelectedEvent != null)
            {
                ArticleSelectedEvent(this, _selectedIndex);
            }
        }

        private void prevBtn_Click(object sender, RoutedEventArgs e)
        {
            ListPager lp = (ListPager)this.FindResource("listPager");
            if (lp.CurrentPageIndex >= 0)
                lp.CurrentPageIndex -= 1;
            if (lp.CurrentPageIndex == 0) prevBtn.IsEnabled = false;
            else prevBtn.IsEnabled = true;
            if (lp.CurrentPageIndex == lp.MaxPageIndex) nextBtn.IsEnabled = false;
            else nextBtn.IsEnabled = true;
        }

        private void nextBtn_Click(object sender, RoutedEventArgs e)
        {
            ListPager lp = (ListPager)this.FindResource("listPager");
            if (lp.CurrentPageIndex <= lp.MaxPageIndex)
                lp.CurrentPageIndex += 1;
            if (lp.CurrentPageIndex == 0) prevBtn.IsEnabled = false;
            else prevBtn.IsEnabled = true;
            if (lp.CurrentPageIndex == lp.MaxPageIndex) nextBtn.IsEnabled = false;
            else nextBtn.IsEnabled = true;
        }
        #endregion

    }
}
